﻿#!/usr/bin/env python
# -*- coding: utf-8 -*-

# pluginmanager.py

# Copyright 2010 Diego Hansen Hahn (aka DiegoHH) <diegohh90 [at] hotmail [dot] com>

# Copyright (C) 2009 Evan Forsmark
# Original concept by Evan Forsmark

# lazynds is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License.

# lazynds is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with lazynds. If not, see <http://www.gnu.org/licenses/>.

from collections import defaultdict

plugins = defaultdict(list)
def register(*events):
    def registered_plugin(funct):
        for event in events:
            plugins[event].append(funct)
        return funct
    return registered_plugin

def trigger_event(event, *args, **kwargs):
    ans = []
    for plugin in plugins[event]:
        ans.append(plugin(*args, **kwargs))
    return ans

# Modificar???!
def load_plugins(config_file):
    with open(config_file, "r") as fh:
        for line in fh:
            line = line.strip()
            if line.startswith("#") or line == "":
                continue
            __import__(line,  globals(), locals(), [], -1)
